//
//  SettingView.swift
//  PokeMaster
//
//  Created by 李腾芳 on 2022/1/10.
//  Copyright © 2022 OneV's Den. All rights reserved.
//

import SwiftUI
import Combine

struct SettingView: View {
    @EnvironmentObject var store: Store
    var settingBinding: Binding<AppState.Settings> {
        $store.appState.settings
    }
    var settings: AppState.Settings {
        store.appState.settings
    }
    
    var body: some View {
        NavigationView {
            List {
                Section {
                    if store.appState.settings.user == nil {
                        Picker("", selection: settingBinding.accountChecker.accountBehavior) {
                            ForEach(AppState.Settings.AccountBehavior.allCases, id: \.self) {
                                Text($0.text)
                            }
                        }
                        .pickerStyle(.segmented)
                        
                        switch settings.accountChecker.accountBehavior {
                        case .login:
                            TextField("电子邮箱", text: settingBinding.accountChecker.email)
                            TextField("密码", text: settingBinding.accountChecker.password)
                            if !store.appState.settings.loginRequested{
                                Button {
                                    print("*****")
                                    store.dispatch(AppAction.settings(.login(email: settings.accountChecker.email, password: settings.accountChecker.password)))
                                } label: {
                                    Text("登陆")
                                }
                            } else {
                                ActivityIndicatorView()
                            }
                            
                        case .register:
                            if settings.emailIsValid {
                                TextField("电子邮箱", text: settingBinding.accountChecker.email)
                                    .foregroundColor(Color.green)
                            } else {
                                TextField("电子邮箱", text: settingBinding.accountChecker.email)
                                    .foregroundColor(Color.red)
                            }
                            
                            TextField("密码", text: settingBinding.accountChecker.password)
                            TextField("再次输入密码", text: settingBinding.accountChecker.verifyPassword)
                            
                            if !settings.registerAccountRequested {
                                Button {
                                    store.dispatch(AppAction.settings(.accountRegister(email: settings.accountChecker.email, password: settings.accountChecker.password)))
                                } label: {
                                    Text("注册")
                                }
                                .disabled(!settings.registerButtonEnable)
                            } else {
                                ActivityIndicatorView()
                            }
                            
                        }
                    } else {
                        Text(store.appState.settings.user!.email)
                        Button {
                            store.appState = AppState.reduce(action: .logout, state: store.appState).0
                        } label: {
                            Text("注销")
                        }
                    }
                } header: {
                    Text("账户")
                }
                
                Section {
                    Toggle("显示英文名", isOn: settingBinding.showEnglishName)
                    Picker("排序方式", selection: settingBinding.sorting) {
                        ForEach(AppState.Settings.Sorting.allCases, id: \.self) {
                            Text($0.text)
                        }
                    }
                    .pickerStyle(.automatic)
                    Toggle("只显示收藏", isOn: settingBinding.showFavoriteOnly)
                    
                    
                } header: {
                    Text("选项")
                }
                
                Section {
                    Button("清空缓存") {
                        store.dispatch(AppAction.settings(.clearCache))
                    }
                    //   Text("清空缓存")
                    .foregroundColor(Color.red)
                }
                
            }
            .navigationTitle("Settings")
            .alert(item: $store.appState.settings.settingsError) { error in
                Alert(title: Text("密码错误"), message: nil, dismissButton: nil)
            }
            .alert(isPresented: settingBinding.alertRegisterDialog) {
                Alert(title: Text("注册完成"), message: nil, dismissButton: nil)
            }
        }
        
        
    }
}

struct SettingView_Previews: PreviewProvider {
    static var previews: some View {
        SettingView().environmentObject(Store())
    }
}
